Qthread seems to be a very difficult thing, especially the signals and slots, there are a lot of people (although users themselves often do not know) in the use of inappropriate (or even wrong) way in using Qthread, casually with Google search, can search out a large number of results. No wonder the QT developer Bradley T. Hughes screaming to you Are-doing-it-wrongLike many users, the first time I saw this,
Overview
If you want to have a deeper understanding of the Qthread in Qt, you need to know these important functions, and now you're going to introduce them. Introduction to Functions
Property
return value
function Body
function
Static
Qthread *
Qthread::currentthread ()
Returns a pointer to the current thread
Qthread inherit Qobject. It can send started and finished signals, and also provides some slot functions.Qobject. Can be used for multiple threads, you can send a signal to call a slot function that exists in another thread, or you can postevent to an object in another thread. This is possible because each thread has its own event loop.Before doing this, it is important to understand that the thread on which the Q
In-depth understanding of QT QthreadBefore understanding Qthread, you need to understand the following Qthread class, Qthread has the following resources (excerpt from the QT 5.1 help document): In the above resources, this article focuses on the slot: start (); signal: Started (), finished () ; protected method: Run (), exec ();Understanding QthreadQthread is ve
[Qt learning] multi-threaded Qthread operations
I,QThread class overview
The QThread class provides a platform-independent way for users to manage multithreading.
# Include
Inherited from QObject class
II,QThread class details
The QThread object manages the control thre
Overview
The Qthread class provides a platform-independent way to manage threads. A Qthread object manages a thread. The execution of Qthread begins with the execution of the run () function, and in the Qthread class of Qt, the run () function initiates the event loop mechanism by invoking the Exec () function, and pr
Let qtimer run in other threads. The general statement is as follows.
1. Specify a timer for the worker thread in main thread.
QThread* thread = new QThread(this);thread->start();QTimer *timer = new QTimer(0);timer->setInterval(100);timer->moveToThread(thread);connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()), Qt::DirectConnection);connect(thread, SIGNAL(started()), timer,SLOT(start()));
Note the
In gemfield's article "from pthread to qthread", we learned about the basic usage of threads, but most of the content was said to be discussed in this article, that is, thread synchronization. Gemfield has a metaphor in "from process to thread", so it is necessary to review it below: * ********************************* to summarize the following: 1. A process is like a person in a house. 2. The clone creation thread is equivalent to adding a person to
Traditional graphical interface applications have only one thread to execute and one operation at a time. If the user invokes a more time-consuming operation, the interface response is frozen.
One solution is to follow the event-handling approach:
call void Qapplication::p rocessevents () or void Qapplication::p rocessevents (int maxtime) to force an event loop, but this approach is potentially risky. According to Qcoreapplication:processevents (), it is possible to cause recursion, result
applications.The solution depends on the purpose of the new thread and the thread lifecycle.
Lifecycle
Development Task
Solution
One call
Run a function in another thread and exit the thread when the function is completed.
Write Functions and useQtconcurrent: RunRun it
Derived qrunnable, useQthreadpool: globalinstance ()-> Start ()Run it
Derive qthread and implement it againQthread: Run (), UseQthread:
Learning Qthread is mainly to imitate the VC under the FTP server to write a QT version. Not much to say,. The software structure of the FTP server has been explained in the above analysis, the solution today is to let each client process can be a thread to run alone. First, the above 3 classes of CPP file, and then give the phenomenon.1, Qlistensocket class1#include"qlistensocket.h"2#include 3#include 4 5Qlistensocket::qlistensocket (Qobject *parent
. Let's talk about the idea first:The timer object needs to run in a thread that turns on the event loop, so we can move it to a thread that has the event loop turned on by the Movetothread () method. Very happy, we quickly encapsulated a timer class, containing a Qtimer and Qthread object, directly using the Qthread object is because my QT version is 4.8.3,run () is not a pure virtual function, the event l
Create a child thread, traverse the folder, and print the information on the main interface.1. Subclass Qthread can generate a new thread and refactor virtual function run (). A child thread communicates with the main thread using the Signal-slot mechanism, which sends the information through the emit signal ("information") to the main threadClass Newthread:public Qthread{Q_objectPublic:newthread (const QSt
First, I would like to quote Bruce Eckel's statement: "to understand the concept of a thread, we should use the process-oriented programming idea instead of the object-oriented programming idea ".
Then, the last two sections of code:
1. Code for slot call
// Threadslottest. h # ifndef threadslottest_h # define threadslottest_h # include
2. Code without slot calls
// Threadslottest. h # ifndef threadslottest_h # define threadslottest_h # include
The two pieces of code are only different from
Workthread, which generally inherits Qthread, creates a temporary workobject in the overloaded run (), which determines whether the workobject is used in the thread
So what if this workobject is a singleton?
Method 2: Use Workobject in Qthread1. Create Workobject1#include 2#include 3#include 4 5 #defineDEBUG (x)6 {7Qdebug () "class Name:""\ n"8"function:""\ n" 9"Thread id ="Qthread::currentthreadid
A qthread example
// Threadtest. h# Ifndef threadtest_h# Define threadtest_h
# Include # Include "test. H"
Class mythread: Public qthread{Protected:Virtual void run ();};
Mythread;
Void test: newslot (){Mythread. Start ();}
# Endif
////////////////////////////////
// Threadtest. cpp
# Include
Void mythread: Run (){For (INT I = 0; I {Printf ("new value I is: % d", I );// Qmessagebox: Information (this, TR (
First, let's talk about the background of the story. When we click a button or something like a control, we may want it to perform complicated operations, however, we do not want the program to be stuck there and wait for the execution of this complex operation to end. multithreading is required here. Although I still don't know how to use multithreading in QT, however, let's make it a summary for future reference.
It is said that it is a good method to implement multithreading on many websites!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.